
import apiType, {
    GET_WAREHOUSE_STORAGE_TYPES_SELECTION,
    GET_INVENTORY_SELECTION,
    GET_INVENTORY_LOSS_LIST,
    GET_WAREHOUSE_STORAGE,
    GET_INVENTORY_LIST
} from "@constants/ActionTypes";

const initialState = {
    storageTypesSelection: [],    //实时库存可选类别
    warehouseStorage: {},    //实时库存
    inventoryLossList: {},    //报损记录
    inventoryList: {},    //盘点记录列表
}

const reformInventorySelectionMap = (data = []) => {
    let _inventorySelectionMap = {}
    data.forEach(item => {
        item.keys.forEach(key => {
            _inventorySelectionMap[key.storage_key] = {
                ...key,
                type_name: item.type_name
            }
        })
    })
    return _inventorySelectionMap
}
export default function update(state = initialState, action) {
    const { response, params, payload } = action
    switch (action.type) {
        case apiType(GET_WAREHOUSE_STORAGE).success:
            return {
                ...state,
                warehouseStorage: response,
            }
        case apiType(GET_WAREHOUSE_STORAGE_TYPES_SELECTION).success:
            return {
                ...state,
                storageTypesSelection: response.types || [],
            }
        case apiType(GET_INVENTORY_SELECTION).success:
            return {
                ...state,
                inventorySelection: response,
                inventorySelectionMap: {
                    ...state.inventorySelectionMap,
                    ...reformInventorySelectionMap(response.data)
                }
            }
        case apiType(GET_INVENTORY_LOSS_LIST).success:
            return {
                ...state,
                inventoryLossList: response,
            }
        case apiType(GET_INVENTORY_LIST).success:
            return {
                ...state,
                inventoryList: response,
            }

        default:
            return state
    }
}